﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EDSDKLib;
using System.Windows.Forms;

namespace SWA_View
{
    public class CamerAction
    {
        #region Delclear

        private static string _picPath = @"d:\SSSRI_TestData\";
        private static uint _whichCam = 1;
        public uint WhichCam
        {
            get { return _whichCam; }
            set { _whichCam = value; }
        }
        static private bool isCameraPreview = false;

        private uint errAll;

        private bool IsCamera1Inited = false;

        static private string _previewPic1 = @"c:\p1.jpg";
        public string PreviewPic1
        {
            get { return _previewPic1; }
            set { _previewPic1 = value; }
        }

        private uint err1;

        static IntPtr ptrCameraList, ptrCamera, ptrCamera1;

        public EdsStateEventHandler stateEventHandler = delegate(uint inEvent, uint inParams, IntPtr inContext)
        {
            switch (inEvent)
            {
                case EDSDK.StateEvent_CaptureError:
                    MessageBox.Show("相机尚未准备好。");
                    break;
                default:
                    break;
            }
            return 0;
        };

        public EdsObjectEventHandler objectEventHandler =
            delegate(uint inEvent, IntPtr inRef, IntPtr inContext)
            {
                switch (inEvent)
                {
                    // in case of a download request
                    case
                        EDSDK.ObjectEvent_DirItemRequestTransfer
                        :
                        DownloadImage(inRef, _picPath, inContext);
                        Console.WriteLine(
                            "Directory Item Created");
                        break;
                    case EDSDK.ObjectEvent_DirItemCreated:
                        //this.invokeNewItemCreatedEvent(new NewItemCreatedEventArgs(getCapturedItem(inRef)));
                        Console.WriteLine(
                            "Directory Item Created");
                        break;
                    default:
                        Console.WriteLine(
                            String.Format(
                                "ObjectEventHandler: event {0}, ref {1}",
                                inEvent.ToString("X"),
                                inRef.ToString()));
                        break;

                }
                return EDSDK.EDS_ERR_OK;
            };

        public EdsObjectEventHandler objectEventHandler1 =
           delegate(uint inEvent, IntPtr inRef, IntPtr inContext)
           {
               switch (inEvent)
               {
                   // in case of a download request
                   case
                       EDSDK.ObjectEvent_DirItemRequestTransfer
                       :
                       DownloadImage(inRef, _picPath, inContext);
                       Console.WriteLine(
                           "Directory Item Created");
                       break;
                   case EDSDK.ObjectEvent_DirItemCreated:
                       //this.invokeNewItemCreatedEvent(new NewItemCreatedEventArgs(getCapturedItem(inRef)));
                       Console.WriteLine(
                           "Directory Item Created");
                       break;
                   default:
                       Console.WriteLine(
                           String.Format(
                               "ObjectEventHandler: event {0}, ref {1}",
                               inEvent.ToString("X"),
                               inRef.ToString()));
                       break;

               }
               return EDSDK.EDS_ERR_OK;
           };
        #endregion
        Form _MainForm = null;
        private  void InitCamera(Form MainForm)
        {
            _MainForm = MainForm;
            errAll = EDSDK.EDS_ERR_OK;
            errAll = EDSDK.EdsInitializeSDK();
            GetCameras(out ptrCameraList);
            err1 = EDSDK.EdsGetChildAtIndex(ptrCameraList, 0, out ptrCamera);

            ///uint* p = &objectEventHandler();
            //inObjectEventHandler = new EDSDK.EdsObjectEventHandler(objectEventHandler);
            //err = EDSDK.EdsSetObjectEventHandler(ptrCamera,
            //                        EDSDK.ObjectEvent_All, objectEventHandler, new IntPtr(0));
            //EdsObjectEventHandler edsObjectEventHandler = new EdsObjectEventHandler(objectEventHandler);

            if (ptrCamera != IntPtr.Zero)
                err1 = EDSDK.EdsSetObjectEventHandler(ptrCamera, EDSDK.ObjectEvent_All, objectEventHandler, ptrCamera);

            if (ptrCamera != IntPtr.Zero)
                err1 = EDSDK.EdsSetCameraStateEventHandler(ptrCamera, EDSDK.StateEvent_All, stateEventHandler,
                                                          IntPtr.Zero);


            if (ptrCamera != IntPtr.Zero)
                err1 = EDSDK.EdsOpenSession(ptrCamera);



            if (errAll == EDSDK.EDS_ERR_OK && err1 == EDSDK.EDS_ERR_OK)
            {
                if (ptrCamera != IntPtr.Zero && err1 == EDSDK.EDS_ERR_OK)
                    err1 = EDSDK.EdsSetPropertyData(ptrCamera, EDSDK.PropID_Evf_OutputDevice, 0,
                                                   (int)EDSDK.EdsDataType.UInt32, EDSDK.EvfOutputDevice_TFT);


            }

            if (errAll == EDSDK.EDS_ERR_OK && err1 == EDSDK.EDS_ERR_OK)
            {
                if (ptrCamera != IntPtr.Zero && err1 == EDSDK.EDS_ERR_OK)
                    err1 = EDSDK.EdsSendCommand(ptrCamera, EDSDK.CameraCommand_ExtendShutDownTimer, 0);

            }

            if (errAll == EDSDK.EDS_ERR_OK && err1 != EDSDK.EDS_ERR_OK)
            {
                MessageBox.Show("无法注册相机1!");
                err1 = EDSDK.EDS_ERR_UNEXPECTED_EXCEPTION;
            }

            if (errAll == EDSDK.EDS_ERR_OK && err1 == EDSDK.EDS_ERR_OK)
                IsCamera1Inited = true;


        }

        static private uint DownloadImage(IntPtr dItem, string path, IntPtr inContext)
        {

            EDSDK.EdsDirectoryItemInfo dirInfo;
            uint err;
            IntPtr stream = new IntPtr();
            err = EDSDK.EdsGetDirectoryItemInfo(dItem, out dirInfo);

            if (err == EDSDK.EDS_ERR_OK)
            {
                if (isCameraPreview)
                {
                    if (inContext == ptrCamera)
                    {
                        _whichCam = 1;
                        err = EDSDK.EdsCreateFileStream(path, EDSDK.EdsFileCreateDisposition.CreateAlways,
                                                        EDSDK.EdsAccess.ReadWrite, out stream);

                    }
                    else if (inContext == ptrCamera1)
                    {
                        _whichCam = 2;
                        err = EDSDK.EdsCreateFileStream(path, EDSDK.EdsFileCreateDisposition.CreateAlways,
                                                        EDSDK.EdsAccess.ReadWrite, out stream);

                    }

                }
                else
                {
                    err = EDSDK.EdsCreateFileStream(path + dirInfo.szFileName,
                                                    EDSDK.EdsFileCreateDisposition.CreateAlways,
                                                    EDSDK.EdsAccess.ReadWrite, out stream);

                }
            }
            if (err == EDSDK.EDS_ERR_OK)
            {
                err = EDSDK.EdsDownload(dItem, dirInfo.Size, stream);
            }
            if (err == EDSDK.EDS_ERR_OK)
            {
                err = EDSDK.EdsDownloadComplete(dItem);
                if (isCameraPreview)
                {
                    //EDSDK.EdsRelease()
                    if (inContext == ptrCamera)
                    {
                        _whichCam = 1;

                        //err = EDSDK.EdsCreateFileStream(@"c:\p1.jpg", EDSDK.EdsFileCreateDisposition.CreateAlways,
                        //                                EDSDK.EdsAccess.ReadWrite, out stream);
                        //CameraPreview camPreview1 = new CameraPreview();
                        System.IO.File.Copy(_previewPic1, @"c:\p1_.jpg", true);
                        //camPreview1.ShowDialog();
                    }


                }
                else
                {


                }
            }
            if (stream != null)
            {
                EDSDK.EdsRelease(stream);
            }
            return err;
        }

        void GetCameras(out IntPtr pCameraList)
        {
            //uint err = EDSDK.EDS_ERR_OK;

            errAll = EDSDK.EdsGetCameraList(out pCameraList);
            if (errAll == EDSDK.EDS_ERR_OK)
            {
                int nCount;
                errAll = EDSDK.EdsGetChildCount(ptrCameraList, out nCount);
                if (nCount == 0)
                {
                    MessageBox.Show("未找到相机。", "EOS_ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    errAll = EDSDK.EDS_ERR_DEVICE_NOT_FOUND;
                }

            }
        }
    }
}
